import tkinter as tk
print(tk.CURRENT)
import traceback
import math
import time
import numpy as np



class Line():
    def __init__(self,canvas:tk.Canvas):
        
        self.canvas = canvas
        self.item_ids=  []
        self.phase=  0
        self.draw()
        self.canvas .after(100,self.refresh)
    def draw(self):
        for x in np.linspace(0,6.29,100):
            x0,y0 = x*100 , math.sin(x)*100+200
            w, h = 5, 5
            x1,x2 = x0-w/2, x0+w/2
            y1,y2 = y0-h/2 , y0+h/2
            item_id = self.canvas. create_oval(x1,y1,x2,y2,fill='black')
            self.item_ids.append(item_id)

    def refresh(self):
        t0 = time.time()
        for i,x in enumerate(np.linspace(0,6.29,100)):
            x0,y0 = x*100 , math.sin(x+self.phase)*100+200
            self.canvas.move_to(self.item_ids[i],x0,y0)
            
        self.phase+=0.1
        self.canvas.after(100,self.refresh)
        t1=time.time()
        print(t1-t0)


class Block():
    '''
    "块"。
    其中的所有内容都会一起移动。
    '''
    def __init__(self, canvas:tk.Canvas):
        self.item_ids = []
        self.canvas = canvas
        
        oval1 = self.canvas. create_oval(80.4,80,50,50,fill='white')
        oval2 = self.canvas. create_oval(180,180,50,50, fill='white')
        self.item_ids.append(oval1)
        self.item_ids.append(oval2)
        text = self.canvas. create_text((180,180),text = 'jhhhhhhhhh' ,fill='black')
        print(text)
        self.set_item_mapping()
        
    def move_to(self, x:float , y:float):
        for id in self.item_ids:
            self.canvas.move_to(id, x,y)
            
    def set_item_mapping(self):
        for id in self.item_ids:
            self.canvas.itemMap[id]=self
    
    def add_item(self,item_id:int):
        self.item_ids.append(item_id)
    
    def del_item(self, item_id:int):
        try:
            self.item_ids.pop(self.item_ids.index(item_id))
        except:
            traceback.print_exc()
            
class MyCanvas(tk.Canvas):
    def __init__(self,parent):
        super().__init__(parent)
        self.itemToMove = None
        self.relativePos = ()
       
        self.itemMap = {}# 东西的映射。当拖动等事件发生时，修改这个字典中的对象。
        self.bind('<ButtonPress-1>',self.on_mouse_down)
        self.bind('<B1-Motion>',self.on_mouse_drag)
    
    def on_mouse_down(self,event):
        self.relativePos=()
        a = self.find_withtag(tk.CURRENT)
        if len(a)>=1:
             coor = self.coords(a[0])
             x,y =  event.x-coor[0],event.y-coor[1]
             self.relativePos = (x,y)
             self.itemToMove  = a[0]
        else:
            self.itemToMove = None
        
    def move_to(self,item_id,x,y):
        pos = self.coords(item_id)
        self.move(item_id, x-pos[0], y-pos[1])
        
    def on_mouse_drag(self,event):
        if not self.itemToMove: # 如果没有要移动的对象，就直接return,防止出现奇奇怪怪的错误。 
            return 
        
        a = self.find_withtag(tk.CURRENT)
        if len(a)>=1:
            self.itemMap[a[0]].move_to(event.x-self.relativePos[0], event.y-self.relativePos[1])
            self.itemconfig(3,text = 'aaaaaaaaaaaaa')

def makeBlock(canvas,node,width=80,height=120):
    b = Block(canvas)
    for p in node.inputPorts:
        port_shape = canvas. create_oval(180,180,50,50, fill='white')
        b.item_ids.append(port_shape)
    pass

if __name__ == '__main__':

    master = tk.Tk()

    w = MyCanvas(master)
    w.pack(fill=tk.BOTH,expand=1)
    #l=Line(w)
    
    b=Block(w)
    
    tk.mainloop()
